import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import local_set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import storage_put from '@dfeidao/atom-web/storage/put';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';
import { spaceid } from '../../atom/config';
import a002 from './a002';
import P01 from './P01';
import P05 from './P05';



// 初始化客户端下拉列表填充
export default async function a001(fd: IFeidaoAiBrowserComponent) {

	// 存储当前登陆用户的productid
	const prid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	await storage_put('productid', prid);
	fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="seachkey123"]').value = '';
	// 加载客户端类型下拉列表
	const res = await nodejs<{ dataArray: Array<{}>, count: number, page_no: number, page_size: number }>('function-definition/zj-000001/s001', { productid: spaceid });
	await render(fd, res, P01, 'P01', 'inner');

	// 获取客户端类型第一项并设为第一次查询的条件
	const client_point_id = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="client_point_id"]').value;
	const res1 = await nodejs<{ dataArray: Array<{ group_number: string }> }>('function-definition/zj-000001/s003', { filter: { productid: prid, client_type: client_point_id } });
	render(fd, res1, P05, 'P05', 'inner');

	// debugger;
	if (res1.dataArray.length > 0) {
		const group_number = res1.dataArray[0].group_number;
		const div_nodes = fd.data.node.querySelectorAll<HTMLDivElement>('[data-name="div"]');
		div_nodes.forEach((v) => {
			const group_numbers = v.getAttribute('group_number');
			if (group_number === group_numbers) {
				set_node_cls(v, 'bg-c', true);
			} else {
				set_node_cls(v, 'bg-c', false);
			}
		});
		local_set(fd, "group_number", group_number);
		await a002(fd);
	} else {
		const div_node = fd.data.node.querySelector<HTMLDivElement>('[data-name="未分组"]');
		set_node_cls(div_node, 'bg-c', true);
		local_set(fd, "group_number", '');
		await a002(fd);
	}

	if (client_point_id) {
		local_set(fd, "client_point_id", client_point_id);
		await a002(fd);
	}
}
